<template src="./index.html"></template>
<style src="./index.scss" lang="scss" scoped></style>

<script>
const Base64 = require('js-base64').Base64;
export default {
    data() {
        return {
            userName: "",
            password: "",
            // 验证码
            verificationCode: "",

            imgData: "",
            token: "",

            message: false,
        }
    },
    created(){
        this.getLogin()
    },
    mounted() {
        let timer = setInterval(()=> {
            this.getLogin()
        }, 60000)
    },
    methods: {
        // 获取验证码
        async getLogin() {
            const res = await this.$http.get({
                isToken: false,
                url: "/api/anon/auth/vercode",
            });
            // console.log(res);
            this.imgData = res.data.data.imageBase64Data;
            this.token = res.data.data.vercodeToken;
        },
        async login() {
            if(this.userName == "") {
                this.$tools.messageError("用户名不能为空！")
                return;
            }
            if(this.password == "") {
                this.$tools.messageError("请输入密码！");
                return;
            }
            if(this.verificationCode == "") {
                this.$tools.messageError("请输入验证码！")
                return;
            }
            // base64转码
            // const baseUserName = Base64.encode(this.userName);
            // const basePassWord = Base64.encode(this.password);
            // const baseVerificationCode = Base64.encode(this.verificationCode);
            // const baseToken = Base64.encode(this.token);

            const baseUserName = this.userName;
            const basePassWord = this.password;
            const baseVerificationCode = this.verificationCode;
            const baseToken = this.token;
            
            // 登录请求
            const res = await this.$http.post({
                isToken: false,
                url: "/api/anon/auth/validate",
                data: {
                    ia: baseUserName,
                    ip: basePassWord,
                    vc: baseVerificationCode,
                    vt: baseToken,
                }
            });
            // 将token提交到state仓库
            this.$store.commit("dataToken", res.data.data.iToken);
            sessionStorage.setItem('store', JSON.stringify(this.$store.state))
            // console.log(res.data.data.iToken);
            console.log();
            if(res.data.code != 0) {
                this.$tools.messageError(res.data.msg);
                return;
            }else {
                this.$router.push({
                    // path: "/home/belonging",
                    // query: {
                    //     id: 111222,
                    //     sid: res.data.data.iToken
                    // },
                    name: "Basic",
                    params: {
                        id: 100,
                        key: res.data.data.iToken
                    }
                });
                this.$tools.messageSuccess("登录成功");
                return;
            }
        },


    },
    computed: {
        
    },


}
</script>

